package com.testmanager.testmananger.foundation.util;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class FileUtils {

      public static  final String BASEPATH = "/opt/uploadfile/";
      public static final String SendSavePath = "/opt/sendsavezip/";

    /**
     * transfer file to str
     * @param path
     * @return
     */
    public static String file2str(String path){

        String encoding = "UTF-8";

        File xmlFile = new File(path);

        InputStreamReader reader = null;
        StringWriter writer = new StringWriter();

        try {
            if (encoding == null || "".equals(encoding.trim())) {
                reader = new InputStreamReader(new FileInputStream(xmlFile), encoding);
            } else {
                reader = new InputStreamReader(new FileInputStream(xmlFile));
            }
            //将输入流写入输出流
            char[] buffer = new char[1024];
            int n = 0;
            while (-1 != (n = reader.read(buffer))) {
                writer.write(buffer, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        return writer.toString();

    }

    /**
     * 删除文件家下所有文件
     * @param dir
     */
    public static void removeDir(File dir) {
        File[] files=dir.listFiles();
        for(File file:files){
            if(file.isDirectory()){
                removeDir(file);
            }else{
               // System.out.println(file+":"+file.delete());
                log.info(file+":"+file.delete());
            }
        }
       // System.out.println(dir+":"+dir.delete());
    }




}
